package com.etcsoft.examples.ui.binding;

import org.eclipse.core.databinding.AggregateValidationStatus;
import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.etcsoft.examples.ui.model.Model;

public class GettingStarted {
    static Model model = new Model();

    static void init(Shell shell) {
        Text text = new Text(shell, SWT.BORDER);
        Label label = new Label(shell, SWT.NONE);

        Button button = new Button(shell, SWT.PUSH);
        button.setText("Double");
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                model.setAmount(model.getAmount() * 2);
            }
        });

        // binding

        IObservableValue modelObservable = BeanProperties.value(Model.class, "amount").observe(
                model);

        DataBindingContext dbc = new DataBindingContext();
        Binding binding = dbc.bindValue(WidgetProperties.text(SWT.Modify).observe(text),
                modelObservable, null, null);
        // 개별 validation
        Label individualErrorLabel = new Label(shell, SWT.NONE);
        dbc.bindValue(SWTObservables.observeText(individualErrorLabel),
                binding.getValidationStatus(), null, null);

        dbc.bindValue(WidgetProperties.text().observe(label), modelObservable, null, null);

        GridLayoutFactory.swtDefaults().generateLayout(shell);

        // 전체 validation

        Label errorLabel = new Label(shell, SWT.NONE);
        dbc.bindValue(SWTObservables.observeText(errorLabel),
                new AggregateValidationStatus(dbc.getBindings(),
                        AggregateValidationStatus.MAX_SEVERITY), null, null);

    }

    public static void main(String[] args) {
        final Display display = new Display();
        Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
            @Override
            public void run() {
                Shell shell = new Shell(display);
                init(shell);
                shell.pack();
                shell.open();
                while (!shell.isDisposed()) {
                    if (!display.readAndDispatch()) {
                        display.sleep();
                    }
                }
                display.dispose();
            }
        });
    }
}
